package com.itcast.dao;

import com.itcast.pojo.User;
import com.itcast.utils.ConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements  UserDao {
    @Override
    public User findUserByUserIdAndPassword(String userId, String password) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            //获取连接
            connection= ConnectionFactory.getConnection();
            //定义sql
            String sql="select * from user where user_id=? and user_password=? ";
            //获取集装箱
            preparedStatement=connection.prepareStatement(sql);
            //给？赋值
            preparedStatement.setString(1,userId);
            preparedStatement.setString(2,password);
            //执行sql
            resultSet=preparedStatement.executeQuery();
            //遍历取值
            while (resultSet.next())
            {
               User user=new User();
               user.setUserId(resultSet.getString("user_id"));
                user.setUserName(resultSet.getString("user_name"));
                user.setUserPassword(resultSet.getString("user_password"));
                user.setUserImage(resultSet.getString("user_image"));
                user.setUserCity(resultSet.getString("user_city"));
                user.setUserEmail(resultSet.getString("user_email"));
                user.setUserGender(resultSet.getString("user_gender"));
                user.setUserTel(resultSet.getString("user_tel"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally{
            ConnectionFactory.close(resultSet,preparedStatement,connection);
        }
        return null;
    }

    @Override
    public void InsertUser(User user) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            //获取连接
            connection=ConnectionFactory.getConnection();
            //定义sql
            String sql="insert into user(user_id,user_name,user_password,user_image) values (?,?,?,?)";
            //获取集装箱
            preparedStatement=connection.prepareStatement(sql);
            //给？赋值
            preparedStatement.setString(1,user.getUserId());
            preparedStatement.setString(2,user.getUserName());
            preparedStatement.setString(3, user.getUserPassword());
            preparedStatement.setString(4, user.getUserImage());
            //执行sql
            int result=preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(resultSet,preparedStatement,connection);
        }
    }

    @Override
    public User findByUserId(String userId) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            //获取连接
            connection= ConnectionFactory.getConnection();
            //定义sql
            String sql="select * from user where user_id=? ";
            //获取集装箱
            preparedStatement=connection.prepareStatement(sql);
            //给？赋值
            preparedStatement.setString(1,userId);

            //执行sql
            resultSet=preparedStatement.executeQuery();
            //遍历取值
            while (resultSet.next())
            {
                User user=new User();
                user.setUserId(resultSet.getString("user_id"));
                user.setUserName(resultSet.getString("user_name"));
                user.setUserPassword(resultSet.getString("user_password"));
                user.setUserImage(resultSet.getString("user_image"));
                user.setUserCity(resultSet.getString("user_city"));
                user.setUserEmail(resultSet.getString("user_email"));
                user.setUserGender(resultSet.getString("user_gender"));
                user.setUserTel(resultSet.getString("user_tel"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally{
            ConnectionFactory.close(resultSet,preparedStatement,connection);
        }
        return null;
    }

    @Override
    public List<User> findAllUser() {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            //获取连接
            connection= ConnectionFactory.getConnection();
            //定义sql
            String sql="select * from user";
            //获取集装箱
            preparedStatement=connection.prepareStatement(sql);
            //给？赋值


            //执行sql
            resultSet=preparedStatement.executeQuery();
            List<User> users=new ArrayList<>();
            //遍历取值
            while (resultSet.next())
            {
                User user=new User();
                user.setUserId(resultSet.getString("user_id"));
                user.setUserName(resultSet.getString("user_name"));
                user.setUserPassword(resultSet.getString("user_password"));
                user.setUserImage(resultSet.getString("user_image"));
              users.add(user);
            }
            return users;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally{
            ConnectionFactory.close(resultSet,preparedStatement,connection);
        }
        return null;
    }

    @Override
    public void UpdateUser(User user) {
       Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            //获取连接
            connection=ConnectionFactory.getConnection();
            //定义sql
            String sql="update user set user_name=?,user_gender=?,user_city=?,user_tel=?,user_email=? where user_name=?";
            //获取集装箱
            preparedStatement=connection.prepareStatement(sql);
            //给？赋值
            preparedStatement.setString(1,user.getUserName());
            preparedStatement.setString(2, user.getUserGender());
            preparedStatement.setString(3, user.getUserCity());
            preparedStatement.setString(4, user.getUserTel());
            preparedStatement.setString(5, user.getUserEmail());
            preparedStatement.setString(6, user.getUserName());
            //执行sql
            int result=preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(resultSet,preparedStatement,connection);
        }   
    }
}
